package com.talent.jiwen.my;

import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.talent.jiwen.R;
import com.talent.jiwen.base.BaseFragment;
import com.talent.jiwen.http.common.ActivityLifeCycleEvent;
import com.talent.jiwen.http.common.ApiFactory;
import com.talent.jiwen.http.common.HttpUtil;
import com.talent.jiwen.http.common.ProgressSubscriber;
import com.talent.jiwen.http.result.StudentInfoResult;
import com.talent.jiwen.ui.widgets.SharePopwindow;
import com.talent.jiwen.util.ImageUtil;
import com.talent.jiwen.util.SPConstant;
import com.talent.jiwen.util.SPUtil;
import com.talent.jiwen.util.Validators;

import org.greenrobot.eventbus.Subscribe;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Observable;

import static com.talent.jiwen.my.WebShowActivity.WEB_CAO_TYPE;
import static com.talent.jiwen.my.WebShowActivity.WEB_INVISIT_TYPE;
import static com.talent.jiwen.my.WebShowActivity.WEB_INVITE_INSTRUCTION;


public class MyFragment extends BaseFragment {
    @BindView(R.id.setIv)
    TextView setIv;
    @BindView(R.id.headIv)
    ImageView headIv;
    @BindView(R.id.nameTv)
    TextView nameTv;
    @BindView(R.id.codeTv)
    TextView codeTv;
    @BindView(R.id.collectLayout)
    RelativeLayout collectLayout;
    @BindView(R.id.myEvaluateLayout)
    RelativeLayout myEvaluateLayout;
    @BindView(R.id.aboutUsLayout)
    RelativeLayout aboutUsLayout;
    @BindView(R.id.couponLayout)
    RelativeLayout couponLayout;
    @BindView(R.id.xinLayout)
    RelativeLayout xinLayout;
    @BindView(R.id.caozuoLayout)
    RelativeLayout caozuoLayout;
    @BindView(R.id.fanLayout)
    RelativeLayout fanLayout;
    @BindView(R.id.moneyTv)
    TextView moneyTv;
    @BindView(R.id.withDrawLayout)
    RelativeLayout withDrawLayout;
    @BindView(R.id.inviteInstructionLayout)
    RelativeLayout inviteInstructionLayout;
    @BindView(R.id.codeLayout)
    RelativeLayout codeLayout;
    @BindView(R.id.invisit_activity)
    ImageView invisit_activity;
    @BindView(R.id.rechargeLayout)
    RelativeLayout rechargeLayout;


    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    public static final String REFRESH_DATA="refresh";

    @Override
    protected void initData() {
        registEventBus();

        nameTv.setText(SPUtil.stringOut(SPConstant.USER_NAME));
        codeTv.setText(getString(R.string.invite_code) + SPUtil.stringOut(SPConstant.INVITE_CODE));

        ImageUtil.loadCircleImage(getContext() , SPUtil.stringOut(SPConstant.USER_HEAD_URL) , headIv);

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getStudentSelfInfo();
            }
        });
        refreshLayout.setEnableLoadMore(false);

        refreshLayout.autoRefresh();

    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Subscribe
    public void onEvent(String flag) {
        if (flag.equals(PersonInfoActivity.REFRESH_INFO)) {
            ImageUtil.loadCircleImage(getContext() , SPUtil.stringOut(SPConstant.USER_HEAD_URL) , headIv);
            nameTv.setText(SPUtil.getUserName());
        }else if(flag.equals(REFRESH_DATA)){
            getStudentSelfInfo();
        }
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_my;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unRegistEventBus();
    }

    @OnClick({R.id.setIv,R.id.codeLayout , R.id.rechargeLayout ,R.id.couponLayout , R.id.collectLayout
            ,R.id.caozuoLayout,  R.id.myEvaluateLayout,R.id.aboutUsLayout,R.id.invisit_activity
            ,R.id.withDrawLayout,R.id.infoLayout,R.id.fanLayout,R.id.xinLayout,R.id.rl_spend_rc
            ,R.id.inviteInstructionLayout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.setIv:
                intentActivity(SetActivity.class);
                break;
            case R.id.codeLayout:
                intentActivity(MyQRCodeActivity.class);
                break;
            case R.id.rechargeLayout:
                intentActivity(RechargeActivity.class);
                break;
            case R.id.couponLayout:
                intentActivity(CouponActivity.class);
                break;
            case R.id.collectLayout:
                intentActivity(CollectListActivity.class);
                break;
            case R.id.caozuoLayout:
                WebShowActivity.WEB_SHOW_TYPE = WEB_CAO_TYPE;
                intentActivity(WebShowActivity.class);
                break;
            case R.id.myEvaluateLayout:
                intentActivity(MyEvaluateActivity.class);
                break;
            case R.id.aboutUsLayout:
                intentActivity(AboutUsActivity.class);
                break;
            case R.id.invisit_activity:
                WebShowActivity.WEB_SHOW_TYPE = WEB_INVISIT_TYPE;
                intentActivity(WebShowActivity.class);
                break;
            case R.id.withDrawLayout:
                intentActivity(WithdrawActivity.class);
                break;
            case R.id.infoLayout:
                intentActivity(PersonInfoActivity.class);
                break;
            case R.id.fanLayout://用户反馈
                intentActivity(FeedbackActivity.class);
                break;
            case R.id.xinLayout://信用中心
                intentActivity(CreditActivity.class);
                break;
            case R.id.rl_spend_rc://资金流水
                intentActivity(RunningWaterActivity.class);
                break;
            case R.id.inviteInstructionLayout:
                WebShowActivity.WEB_SHOW_TYPE = WEB_INVITE_INSTRUCTION;
                intentActivity(WebShowActivity.class);
                break;
        }
    }

    /**获取账户余额
     *
     */
    private void getStudentSelfInfo(){
        Map<String, String> map = new HashMap<>();
        map.put("studentId" , SPUtil.stringOut(SPConstant.USER_ID));

        Observable observable = ApiFactory.getEducationApiSingleton().getStudentSelfInfo(SPUtil.stringOut(SPConstant.TOKEN) , map);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<StudentInfoResult>(getActivity()) {
            @Override
            protected void _onNext(StudentInfoResult studentInfoResult) {
//                SPUtil.intIn(SPConstant.ID_CARD_BIND_STATUS , studentInfoResult.getStudentInfo().getBindStatus());
//                SPUtil.stringIn(SPConstant.TOTAL_AMOUNT  , studentInfoResult.getStudentInfo().getTotalMoney());
                if(refreshLayout!=null){
                    refreshLayout.finishRefresh();
                }
                String totalMoney = studentInfoResult.getStudentInfo().getTotalMoney();
                if(Validators.isEmpty(studentInfoResult.getStudentInfo().getTotalMoney())){
                    moneyTv.setText("0.0");
                }else{
                    moneyTv.setText(totalMoney);
                }
            }

            @Override
            protected void _onError(String message) {
                if(refreshLayout!=null){
                    refreshLayout.finishRefresh();
                }
                showToast(message);
            }
        }, ActivityLifeCycleEvent.DESTROY, lifecycleSubject,false);
    }
}
